package com.one.rope.mvp.basecore.util;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collection;
import java.util.Locale;
import java.util.UUID;

/**
 * String工具类
 */
public class StringUtils {

  public final static String NULL_STRING = "\0";
  public final static String NEW_LINE = "\r";

  /**
   * 是否为数字
   */
  public static boolean isNumeric(String str) {
    if (str == null) {
      return false;
    }
    for (int i = 0; i < str.length(); i++) {
      if (!Character.isDigit(str.charAt(i))) {
        return false;
      }
    }
    return true;
  }

  public static int length(String source) {
    if (source == null) {
      return 0;
    }
    return source.length();
  }

  public static boolean contains(String source, String contains) {
    return contains(source, contains, true);
  }

  public static boolean contains(String source, String contains, boolean ignoreCase) {
    if (source == null || contains == null) {
      return false;
    }
    if (ignoreCase) {
      return source.toLowerCase().indexOf(contains.toLowerCase()) >= 0;
    } else {
      return source.indexOf(contains) >= 0;
    }
  }

  /**
   * 是否为空白字符
   */
  public static boolean isWhiteSpace(String str) {
    if (str == null) {
      return true;
    }
    for (int i = 0; i < str.length(); i++) {
      if (!Character.isWhitespace(str.charAt(i))) {
        return false;
      }
    }
    return true;
  }

  public static String stringForObject(Object obj) {
    if (obj == null) {
      return "";
    }
    return obj.toString();
  }

  public static String trim(String str) {
    if (str == null) {
      return "";
    }
    return str.trim();
  }

  public static String trim(String str, char ch) {
    if (str == null) {
      return "";
    }
    String result = trimLeft(str, ch);
    result = trimRight(result, ch);
    return result;
  }

  public static String trim(String str, String chs) {
    if (str == null) {
      return "";
    }
    String result = trimLeft(str, chs);
    result = trimRight(result, chs);
    return result;
  }

  public static String upperLeftOne(String str) {
    if (isNullOrEmpty(str)) {
      return "";
    }
    return str.substring(0, 1).toUpperCase() + str.substring(1);
  }

  public static String trimLeft(String str, char ch) {
    if (str == null) {
      return "";
    }
    String result = str;
    while (result.length() > 0 && result.charAt(0) == ch) {
      result = result.substring(1);
    }
    return result;
  }

  public static String trimLeft(String str, String chs) {
    if (str == null) {
      return "";
    }
    if (str.length() < chs.length()) {
      return str;
    }
    String result = str;
    while (result.length() > 0 && result.indexOf(chs) == 0) {
      result = result.substring(chs.length());
    }
    return result;
  }

  public static String trimLeftOne(String str, String chs) {
    if (str == null) {
      return "";
    }
    if (str.length() < chs.length()) {
      return str;
    }
    String result = str;
    if (result.length() > 0 && result.indexOf(chs) == 0) {
      result = result.substring(chs.length());
    }
    return result;
  }

  public static String trimRight(String str, char ch) {
    if (str == null) {
      return "";
    }
    String result = str;
    while (result.length() > 0 && result.charAt(result.length() - 1) == ch) {
      result = result.substring(0, result.length() - 1);
    }
    return result;
  }

  public static String trimRight(String str, String chs) {
    if (str == null) {
      return "";
    }
    if (str.length() < chs.length()) {
      return str;
    }
    String result = str;
    while (result.length() > 0 && result.lastIndexOf(chs) == (result.length() - chs.length())) {
      result = result.substring(0, result.length() - chs.length());
    }
    return result;
  }

  public static String trimRightOne(String str, String chs) {
    if (str == null) {
      return "";
    }
    if (str.length() < chs.length()) {
      return str;
    }
    String result = str;
    if (result.length() > 0 && result.lastIndexOf(chs) == (result.length() - chs.length())) {
      result = result.substring(0, result.length() - chs.length());
    }
    return result;
  }

  public static String trimLeftOne(String str, char ch) {
    if (str == null) {
      return "";
    }

    String result = str;
    if (result.length() > 0 && result.charAt(0) == ch) {
      result = result.substring(1);
    }
    return result;
  }

  public static String trimRightOne(String str, char ch) {
    if (str == null) {
      return "";
    }

    String result = str;
    if (result.length() > 0 && result.charAt(result.length() - 1) == ch) {
      result = result.substring(0, result.length() - 1);
    }
    return result;
  }

  public static String guid() {
    return UUID.randomUUID().toString().replaceAll("-", "");
  }

  public static String escapeJSON(String jsonString) {
    if (jsonString == null) {
      return "";
    }
    jsonString = jsonString.replaceAll("'", "\\'");
    return jsonString;
  }

  public static InputStream stringToInputStream(String str) throws IOException {
    return new ByteArrayInputStream(str.getBytes());
  }

  public static boolean isNullOrEmpty(String string) {
    return string == null || string.equals("");
  }

  public static boolean isNullOrEmptyTrim(String string) {
    if (string == null) {
      return true;
    }
    string = string.trim();
    return string.length() == 0 ? true : false;
  }

  public static String isNullSetEmpty(String string) {
    return string == null ? "" : string;
  }

  public static String isNullOrEmptySet(String string, String defalutValue) {
    if (isNullOrEmpty(string)) {
      return defalutValue;
    }
    return string;
  }

  public static String substring(String source, int length) {
    if (source == null) {
      return "";
    }
    if (source.length() > length) {
      return source.substring(0, length);
    }
    return source;
  }

  public static String substring(String source, int length, int start) {
    if (source == null) {
      return "";
    }
    if (source.length() > (length + start)) {
      return source.substring(start, length);
    }
    return source.substring(start);
  }

  public static String substring(String source, String prevStr) {
    return substring(source, prevStr, null, false);
  }

  public static String substring(String source, String prevStr, String endStr) {
    return substring(source, prevStr, endStr, false);
  }

  public static String substring(String source, String prevStr, String endStr, boolean ignoreCase) {
    if (source == null) {
      return "";
    }
    if (ignoreCase) {
      source = source.toLowerCase(Locale.US);
      prevStr = prevStr.toLowerCase(Locale.US);
      if (endStr != null) {
        endStr = endStr.toLowerCase(Locale.US);
      }
    }
    int prevIndex = source.indexOf(prevStr);
    if (prevIndex == -1) {
      return "";
    }
    int newStart = prevIndex + prevStr.length();
    if (newStart >= source.length()) {
      return "";
    }

    if (endStr != null) {
      int endIndex = source.indexOf(endStr, newStart);
      if (endIndex == -1) {
        return "";
      }
      if (endIndex <= newStart) {
        return "";
      }
      return source.substring(newStart, endIndex);
    } else {
      return source.substring(newStart);
    }

  }

  /**
   * 数据转换为不同的进制
   */
  public static String digitToOther(int n, int b) {
    String str = "", digitChar = "0123456789abcdef";
    if (n == 0) {
      return "";
    } else {
      str = digitToOther(n / b, b);
      return str + digitChar.charAt(n % b);
    }
  }

  public static String[] split(String source, String splitStr) {
    if (source == null) {
      return new String[]{};
    }
    return org.apache.commons.lang.StringUtils.split(source, splitStr);
  }

  public static String replace(String source, String searchString, String replacement) {
    if (source == null) {
      return "";
    }
    return org.apache.commons.lang.StringUtils.replace(source, searchString, replacement);
  }

  public int indexOf(String source, String match) {
    if (source == null || match == null) {
      return -1;
    }
    return source.toLowerCase().indexOf(match.toLowerCase());
  }

  public static String firstCharacterToUpper(String srcStr) {
    return srcStr.substring(0, 1).toUpperCase() + srcStr.substring(1);
  }

  public static String replaceUnderlineAndfirstToUpper(String srcStr) {
    return replaceUnderlineAndfirstToUpper(srcStr, "_", "");
  }

  /**
   * 替换字符串并让它的下一个字母为大写
   */
  public static String replaceUnderlineAndfirstToUpper(String srcStr, String org, String ob) {
    String newString = "";
    int first = 0;
    while (srcStr.indexOf(org) != -1) {
      first = srcStr.indexOf(org);
      if (first != srcStr.length()) {
        newString = newString + srcStr.substring(0, first) + ob;
        srcStr = srcStr.substring(first + org.length(), srcStr.length());
        srcStr = firstCharacterToUpper(srcStr);
      }
    }
    newString = newString + srcStr;
    return newString;
  }

  public static boolean equals(String str1, String str2) {
    return equals(str1, str2, false);
  }

  /**
   * 比较字符串,null和""相等
   *
   * @param str1 字符串1
   * @param str2 字符串2
   * @param ignoreCase 是否忽略大小写
   * @return boolean 是否
   */
  public static boolean equals(String str1, String str2, boolean ignoreCase) {
    str1 = isNullSetEmpty(str1);
    str2 = isNullSetEmpty(str2);
    return ignoreCase ? str1.equalsIgnoreCase(str2) : str1.equals(str2);
  }

  /**
   * Byte数组转换成字符串
   *
   * @param bytes byte[]
   * @return String
   */
  public static String bytesToHexString(byte[] bytes) {
    String hex = "";
    for (int i = 0; i < bytes.length; i++) {
      hex += byteToHexString(bytes[i]);
    }
    return hex;
  }

  /**
   * byte转换成字符串
   *
   * @param b byte
   * @return String
   */
  public static String byteToHexString(byte b) {
    char[] digit = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
    char[] tempArr = new char[2];
    tempArr[0] = digit[(b >>> 4) & 0x0F];
    tempArr[1] = digit[b & 0x0F];

    String res = new String(tempArr);

    return res;
  }

  /**
   * list拼接成字符隔开的字符串
   */
  public static String listToStringAddSeprator(Collection<?> list, String separator) {
    if (null == list) {
      return "";
    }
    return org.apache.commons.lang.StringUtils.join(list, separator);
  }

  /**
   * list拼接成字符隔开的字符串
   */
  public static String listToStringAddSeprator(String[] list, String separator) {
    if (null == list) {
      return "";
    }
    return org.apache.commons.lang.StringUtils.join(list, separator);
  }
}
